﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebUtils.Models;
using Av.Utils;
using System.Text.RegularExpressions;
using System.Globalization;

namespace WebUtils.Forms
{
    public partial class SchemaForm : Form
    {

        protected string[] _schemaText = null;
        protected SchemaFormat _schemaFormat = null;

        // Whitespaces and field name enclosed in '`'
        protected Regex _rxField = new Regex(@"([\s]*)(`)([\w\d_]+)(`)");

        public SchemaForm()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            lstSchemaFormats.Items.AddRange(SchemaFormat.Items.ToArray());
        }

        private void OnSchemaTextChanged(object sender, EventArgs e)
        {
            _schemaText = (sender as TextBox).Lines;
        }

        private void OnSchemaFormatChanged(object sender, EventArgs e)
        {
            _schemaFormat = (sender as ComboBox).SelectedItem as SchemaFormat;
        }

        private void OnConvertToSchemaClicked(object sender, EventArgs e)
        {
            List<DbField> fields = new List<DbField>();
            Match m;
            string s;

            for(int i = 0; i < _schemaText.Length; i++)
            {
                s = _schemaText[i].Trim();
                m = _rxField.Match(_schemaText[i]);
                if(m.Success)
                {
                    DbField f = new DbField();
                    s = f.ExtractFieldName(s);
                    s = f.ExtractFieldType(s);
                    s = f.ExtractIsNotNull(s);

                    if(f.FieldTypeIso != DbTypeIso.__NOT_SET__)
                    {
                        Log4cs.Log("Parsed DB field: {0}", f.FullInfo);
                        fields.Add(f);
                    }
                }
            }

            Log4cs.Log("Got {0} DB fields out of {1} lines", fields.Count, _schemaText.Length);
            PrepareSchemaFromDbField(fields);
        }

        private void PrepareSchemaFromDbField(List<DbField> fields)
        {
            StringBuilder sb = new StringBuilder();
            string s = "";
            foreach(var f in fields)
            {
                s = string.Concat("        ", f.FieldName, ":");
                if(s.Length < 32)
                {
                    sb.AppendFormat("{0,-32}{1}{2}", s, ConvertDbFieldTypeToPropel(f), f.LengthPrecision);
                }
                else
                {
                    sb.AppendFormat("{0} {1}{2}", s, ConvertDbFieldTypeToPropel(f), f.LengthPrecision);
                }
                sb.AppendLine();
            }

            txtOutput.Text = sb.ToString();
        }

        private string ConvertDbFieldTypeToPropel(DbField f)
        {
            switch(f.FieldTypeIso)
            {
                case DbTypeIso.INTEGER:
                    return "INT";
                case DbTypeIso.SMALLINT:
                    return "INT";
                case DbTypeIso.NUMERIC:
                    break;
                case DbTypeIso.DECIMAL:
                    break;
                case DbTypeIso.REAL:
                    break;
                case DbTypeIso.DOUBLE_PRECISION:
                    break;
                case DbTypeIso.FLOAT:
                    break;
                case DbTypeIso.BINARY:
                    break;
                case DbTypeIso.BINARY_VARYING:
                    break;
                case DbTypeIso.BINARY_LARGE_OBJECT:
                    break;
                case DbTypeIso.BOOLEAN:
                    break;
                case DbTypeIso.CHARACTER:
                    break;
                case DbTypeIso.CHARACTER_LARGE_OBJECT:
                    break;
                case DbTypeIso.NATIONAL_CHARACTER:
                    break;
                case DbTypeIso.NATIONAL_CHARACTER_VARYING:
                    break;
                case DbTypeIso.NATIONAL_CHARACTER_LARGE_OBJECT:
                    break;
                case DbTypeIso.DATE:
                    break;
                case DbTypeIso.TIME_WITHOUT_TIMEZONE:
                    break;
                case DbTypeIso.TIMESTAMP_WITHOUT_TIMEZONE:
                    break;
                case DbTypeIso.TIME_WITH_TIMEZONE:
                    break;
                case DbTypeIso.TIMESTAMP_WITH_TIMEZONE:
                    break;
            }

            return f.FieldTypeIso.ToString();
        }

    }
}
